'use strict'
import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
import store from '../store/store'

axios.defaults.baseURL = process.env.NODE_ENV === "production"?'http://pjih2x.natappfree.cc/':'http://pjih2x.natappfree.cc/'

//请求时的拦截
axios.interceptors.request.use(config => {
  // loading
  // if (store.state._token) {
  //   config.headers['X-Token'] = store.state._token; // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
  // }
  return config
}, error => {
    //请求异常时做一些处理
  return Promise.reject(error)
})
//响应时的拦截
axios.interceptors.response.use(response => {
  let json = response && response.data
  if(json.code == 1 ){
    return response
  }
  throw new Error(json.msg || '系统繁忙')

}, error => {
    //当响应异常时，做一些处理
    return Promise.resolve(error.response)
})

axios._post = function(url, data){
  return axios({
    method: 'post',
    baseURL: 'http://47.98.160.66:9000/',
    url,
    data: qs.stringify(data),
    timeout: 10000,
    headers: { 
      'withCredentials':true,
      'Access-Control-Allow-Credentials':true,
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
      'Authorization':"token"+store.state._token
    }
  })
}


axios._get = function(url, params){
  return axios({
    method: 'get',
    baseURL: 'http://103.12.234.196:8000/',
    url,
    params, // get 请求时带的参数
    timeout: 10000,
    headers: {
      'X-Requested-With': 'XMLHttpRequest'
    }
  })
}

Vue.prototype.$axios = axios;

